package com.sdtele.tta.operation;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.models.VoteOption;
import com.sdtele.tta.models.VoteScore;
import com.sdtele.tta.models.VoteTask;

public class OperationGetVoteScore extends Operation {

	public JsonResponse handle(JsonRequest jsonRequest) {
		// System.out.println("OperationLogin:req=" + jsonRequest);
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		if (curUser == null) {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
			return jres;
		}
		Map<String, Object> voteMap = new HashMap<String, Object>();
		long voteId = jsonRequest.getJsonObject().get("voteId").getAsLong();
		VoteTask voteTask = Dac.getInstance().getVoteTaskbyVoteid(voteId);
		User voteOwner = Dac.getInstance().getUser(voteTask.getOwnerId());
		List<User> voteImpList = Dac.getInstance().getVoteOperationbyImpelement(voteId);
		List<User> voteReaList = Dac.getInstance().getVoteOperationbyReader(voteId);
		List<VoteOption> voteOpList = Dac.getInstance().getVoteOption(voteId);
		List<Map<String, Object>> voteScore = new ArrayList<Map<String, Object>>();
		List<User> voteNoImpList = Dac.getInstance().getVoteOperationbyImpelement(voteId);
		List<VoteScore> vList = Dac.getInstance().getVotescore(voteId);
		Set<Long> userSet = new HashSet<Long>();
		if (vList != null) {
			int index = 0;
			for (VoteScore vScore : vList)
				userSet.add(vScore.getUserId());
			// System.out.println(userSet);
			// System.out.println("投票人："+voteImpList);
			// System.out.println("未投票人："+voteNoImpList);
			for (User u : voteImpList) {
				if (userSet.contains(u.getId())) {
					voteNoImpList.remove(index);
					index--;
				}
				index++;
			}
		}
		DecimalFormat df = new DecimalFormat("0.00");
		if (voteOpList != null)
			for (VoteOption v : voteOpList) {
				Map<String, Object> scoreMap = new HashMap<String, Object>();
				scoreMap.put("voteOptionId", v.getId());
				scoreMap.put("voteOptionName", v.getVoteOptionName());
				if (vList != null) {
					double avg = Dac.getInstance().getVoteavg(voteId, v.getId());
					scoreMap.put("sum", Dac.getInstance().getVotesum(voteId, v.getId()));
					scoreMap.put("avg", df.format(avg));
				} else {
					scoreMap.put("sum", 0);
					scoreMap.put("avg", 0);
				}
				voteScore.add(scoreMap);
			}
		voteMap.put("voteTask", voteTask.ToVoteTaskOut());
		voteMap.put("voteOwner", voteOwner);
		if (voteImpList != null && !voteImpList.equals(null))
			voteMap.put("voteImpList", voteImpList);
		if (voteReaList != null && !voteReaList.equals(null))
			voteMap.put("voteReaList", voteReaList);
		voteMap.put("voteScore", voteScore);
		if (voteNoImpList != null && voteNoImpList.size() > 0)
			voteMap.put("voteNoImpList", voteNoImpList);
		jres.setStatus(0);
		jres.setMessage("当前任务投票结果信息");
		jres.setEntity(voteMap);
		return jres;

	}

}